import {defineStore} from 'pinia'
import cache from "@/plugins/cache.ts";

const useSearchStore = defineStore('search', {
    state: () => ({
        searchList: [
            {
                key: 'baidu',
                title: '百度搜索',
                icon: 'baidu',
                href: 'http://www.baidu.com/s?wd=',
                translate: 'https://fanyi.baidu.com/translate?#zh/en/',
            },
            {
                key: 'bing',
                title: '必应搜索',
                icon: 'bing',
                href: 'https://cn.bing.com/search?q=',
                translate: 'https://fanyi.baidu.com/translate?#zh/en/',
            },
            {
                key: 'google',
                title: 'google搜索',
                icon: 'google',
                href: 'https://www.google.com/search?q=',
                translate:
                    'https://translate.google.cn/#view=home&op=translate&sl=en&tl=zh-CN&text=',
            },
        ],
        // 当前搜索栏的设置
        searchSetting: {
            // 状态
            status: 'install',
            // 是否显示搜索栏
            show: true,
            // 当前搜索栏的高度
            height: 50,
            // 当前搜索栏的宽度
            width: 100,
            // 搜索栏的圆角
            radius: 0,
            // 搜索栏透明
            opacity: 0.5,
            // 是否展示搜索历史
            showHistory: true,
            // 是否需要翻译
            translate: true,
            // 默认翻译引擎
            translateEngine: 'baidu',
        },
        // 搜索历史
        searchHistory: [],
    }),
    actions: {
        /**
         * 保存搜索框设置
         */
        saveSearchSetting() {
            // 保存到本地
            cache.local.setObject("ST-searchSetting", this.searchSetting)
            // TODO Ajax
        },
    },
    getters: {
        /**
         * 获取搜索框设置
         */
        getSearchSetting(): any {
            console.log("getSearchSetting121")
            // 从本地获取
            if(this.searchSetting.status == 'install'){
                // 第一次加载，先获取本地的，本地没有获取云端的
                const searchSetting = cache.local.getObject("ST-searchSetting")
                if (searchSetting) {
                    this.searchSetting = searchSetting
                    this.searchSetting.status = 'local' // 在本地有
                    return this.searchSetting
                }
                return this.searchSetting
            }
            else if(this.searchSetting.status == 'local'){
                return this.searchSetting
            }
        },

    }
})

export default useSearchStore
